﻿using DataBroker.Criteria;
using DataBroker.Dal;
using DataBroker.Dao;
using NorthwindDAO;
using NUnit.Framework;
using System.Collections.Generic;

namespace D2BkTest
{
    [TestFixture]
    public class FilterTest
    {
        [SetUp]
        public void SetUp()
        {
            D2Bk.ChangeDatabase("Northwind");
        }
        [Test]
        public void AddAndIsNull()
        {
            Filter f    = new Filter();
            f.Add("Fax", Logical.And, Operator.IsNull);
            List<Entity> customers = D2Bk.Fetch(f, new Customers());
            Assert.AreEqual(22, customers.Count);
        }
        [Test]
        public void AddAndIsNotNull()
        {
            Filter f = new Filter();
            f.Add("Fax", Logical.And, Operator.IsNotNull);
            List<Entity> customers = D2Bk.Fetch(f, new Customers());
            Assert.AreEqual(69, customers.Count);
        }
        [Test]
        public void Distinct()
        {
            Filter f = new Filter(new string[] { "ContactTitle" });
            f.Distinct = true;
            List<Entity> customers = D2Bk.Fetch(f, new Customers());
            Assert.AreEqual(13, customers.Count);
        }
        [Test]
        public void AddProjection()
        {
            Filter f = new Filter();
            f.AddProjection(new string[] { "ContactTitle" });
            f.Distinct = true;
            List<Entity> customers = D2Bk.Fetch(f, new Customers());
            Assert.AreEqual(13, customers.Count);
        }
        [Test]
        public void AddProjection2()
        {
            Filter f = new Filter();
            f.AddProjection("ContactTitle");
            f.Distinct = true;
            List<Entity> customers = D2Bk.Fetch(f, new Customers());
            Assert.AreEqual(13, customers.Count);
        }
        [Test]
        public void RowsPerPage()
        {
            Filter f = new Filter();
            f.RowsPerPage = 10;
            List<Entity> customers = D2Bk.Fetch(f, new Customers());
            Assert.AreEqual(10, customers.Count);
        }
        [Test]
        public void RowsPerPagePageNumber() 
        {
            Filter f = new Filter();
            f.RowsPerPage = 11;
            f.PageNumber = 1;
            List<Entity> firstListOfCustomers = D2Bk.Fetch(f, new Customers());
            Assert.AreEqual(11, firstListOfCustomers.Count);
            f = new Filter();
            f.RowsPerPage = 10;
            f.PageNumber = 2;
            List<Entity> secondListOfCustomers = D2Bk.Fetch(f, new Customers());
            Assert.AreEqual(((Customers)firstListOfCustomers[10]).Customerid, ((Customers)secondListOfCustomers[0]).Customerid);
        }
        [Test]
        public void Nested()
        {
            Filter f1 = new Filter();
            f1.Add("ContactTitle","Sales");
            f1.Add("City", "Berlin");
            Filter f2 = new Filter();   
            f2.Add("ContactTitle", "Owner");
            f2.Add("City", "San Francisco");
            f1.AddNested(f2, Logical.Or);
            List<Entity> customers = D2Bk.Fetch(f1  , new Customers());
            Assert.AreEqual(1, customers.Count);
        }
    }
}
